package router

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"kill/client/client"
	"kill/client/enit"
	"kill/common/models/mysql"
	"kill/common/proto/goods"
	"strconv"
)

func AddKill(c *gin.Context) {
	var data enit.AddKill
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	kill, err := client.AddKill(c, &goods.AddKillReq{
		Id:           data.Id,
		GoodsNum:     int64(data.GoodsNum),
		CurrentPrice: float32(data.CurrentPrice),
		StartTime:    data.StartTime,
		EndTime:      data.EndTime,
	})
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 0,
		"data": kill,
	})
}
func EsToKill(c *gin.Context) {
	kill, err := client.EsToKill(c, &goods.EsToKillReq{})
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 0,
		"data": kill,
	})
}
func KillSearch(c *gin.Context) {
	var data enit.KillSearch
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	search, err := client.KillSearch(c, &goods.KillSearchReq{GoodsName: data.GoodsName})
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 0,
		"data": search,
	})
}
func Register(c *gin.Context) {
	var data enit.Register
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	register, err := client.Register(c, &goods.RegisterReq{
		Username: data.Username,
		Password: data.Password,
	})
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 0,
		"data": register,
	})
}
func Login(c *gin.Context) {
	var data enit.Login
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	login, err := client.Login(c, &goods.LoginReq{
		Username: data.Username,
		Password: data.Password,
	})
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 0,
		"data": login,
	})
}
func RealName(c *gin.Context) {
	var data enit.RealName
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	userId := c.GetUint("userId")
	fmt.Println(userId)
	realname, err := client.Realname(c, &goods.RealNameReq{
		Name:   data.Name,
		IdCard: data.IdCard,
		UserId: int64(userId),
	})
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 0,
		"data": realname,
	})
}
func CreateOrder(c *gin.Context) {
	var data enit.CreateOrder
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	userId := c.GetUint("userId")
	order, err := client.CreateOrder(c, &goods.CreateOrderReq{
		UserId:  int64(userId),
		GoodsId: data.GoodsId,
		Num:     data.Num,
	})
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 0,
		"data": order,
	})
}
func AliPay(c *gin.Context) {

}
func AliPayBack(c *gin.Context) {
	fmt.Println("回调成功")

	fmt.Println(c.PostForm("trade_status"))
	fmt.Println(c.PostForm("out_trade_no"))
	c.JSON(200, gin.H{
		"code": 0,
		"data": "成功",
	})
	orderSn := c.GetString("out_trade_no")

	status := c.GetString("trade_status")

	if status == "TRADE_SUCCESS" {
		status = "1" //支付成功
	}
	if status == "WAIT_BUYER_PAY" {
		status = "2" // 未支付，等待支付
	}
	if status == "TRADE_CLOSED" {
		status = "3" //已退款
	}
	if status == "TRADE_FINISHED" {
		status = "4" //交易结束
	}
	Status, _ := strconv.Atoi(status)
	o := mysql.Order{}
	o.UpdateOrderStatus(orderSn, Status)
}
